home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7361 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: camelot.dsccc.com!not-for-mail
  2. From: kcline@sun152.spd.dsccc.com (Kevin Cline)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ & macros
  5. Date: 22 Feb 1996 15:47:46 -0600
  6. Organization: DSC Communications Corporation Switch Products Division
  7. Message-ID: <4gioa2$nsb@sun152.spd.dsccc.com>
  8. References: <DLBxoH.GLw@ariel.cs.yorku.ca> <310EA2DE.6927@a4430edc.esr.hp.com> <4eun5f$50c@cloner4.netcom.com> <4gg06d$401m@news-s01.ny.us.ibm.net>
  9. NNTP-Posting-Host: sun152.spd.dsccc.com
  10.  
  11. In article <4gg06d$401m@news-s01.ny.us.ibm.net>,  <bbogard@ibm.net> wrote:
  12. >In <4eun5f$50c@cloner4.netcom.com>, swampwiz@ix.netcom.com(Jean P. Laborde ) writes:
  13. >>
  14. >>Someone responded to this group and advocated using a define macro.
  15. >>
  16. >>I thought that was extremely bad practice in C++.
  17. >>
  18. >>Comments Encouraged,
  19. >>The Swamp Wizard
  20. >
  21. >Using #define macros is not a bad idea, but it depends on the problem and restrictions
  22. >of the project you are working on.  Sometime, for speed you want just a macro and not
  23. >have to make a function call.
  24.  
  25. Using macros to define functions is a very bad idea.  Use inline functions
  26. instead.  This allows the compiler to decide whether inlining the code
  27. is faster than a function call.  The compiler knows more about this than
  28. the programmer.  Also, inline functions calls are typesafe.
  29.  
  30. Macros should only be used for text-processing tricks that improve
  31. code maintainability, e.g.
  32.  
  33.     #define FRUIT_LIST \
  34.         FRUIT(apple), \
  35.         FRUIT(orange), \
  36.         FRUIT(pear)
  37.  
  38.     #define FRUIT(t) t
  39.     typedef enum { ITEM_LIST } Fruit;
  40.     #undef FRUIT
  41.  
  42.     // #t produces a quoted string 
  43.     #define FRUIT(t) #t
  44.     static const char* const FruitNameTable[] = { ITEM_LIST };
  45.     #undef ITEM
  46.  
  47. When the list gets long, techniques like this can be useful for
  48. reducing redundancies in source code.  I find that as soon
  49. as I have to specify the same thing twice, and the compiler is unable
  50. to make sure they match, they won't. 
  51. -- 
  52. Kevin Cline
  53.